﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

using Castle.Core.Logging;

using InfoGenerateSystem.Persistence.IDao;
using InfoGenerateSystem.Core.Utils;
using InfoGenerateSystem.Domain;

namespace InfoGenerateSystem.WebView
{
    /// <summary>
    /// 表格图截图页面
    /// 
    /// 创建人:     王昊
    /// 创建时间:   2009-05-12
    /// 
    /// </summary>
    public partial class TableChart : System.Web.UI.Page
    {
        private IGenericDao _dao = ServiceHelper.GetService(typeof(IGenericDao)) as IGenericDao;
        private ILogger _log = ServiceHelper.GetService(typeof(ILogger)) as ILogger;

        private String labelID;
        private String LabelID
        {
            get
            {
                labelID = Request.QueryString["labelID"];
                return labelID;
            }
        }

        private String stkCode;
        private String StkCode
        {
            get
            {
                stkCode = Request.QueryString["stkCode"];
                return stkCode;
            }
        }

        private Int64 accountCode;
        private Int64 AccountCode
        {
            get
            {
                if (!String.IsNullOrEmpty(Request.QueryString["accountCode"]))
                {
                    accountCode = Convert.ToInt64(Request.QueryString["accountCode"]);
                }
                else 
                {
                    accountCode = 0;
                }
                return accountCode;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            try
            {
                if (!String.IsNullOrEmpty(LabelID))
                {
                    TblbLabelCase lblCase = _dao.Find("TblbLabelCase.FindByLABELID", labelID) as TblbLabelCase;
                    String procedureName = lblCase.SQLSTR;
                    if (!procedureName.StartsWith("EXEC", StringComparison.OrdinalIgnoreCase))
                    {
                        procedureName = "EXEC " + procedureName;
                    }
                    if (procedureName.Contains("@"))
                    {
                        procedureName = procedureName.Substring(0, procedureName.IndexOf('@'));
                    }

                    Hashtable htParams = new Hashtable();
                    htParams["labelID"] = LabelID;
                    htParams["procedureName"] = procedureName;

                    if (!String.IsNullOrEmpty(StkCode))
                    {
                        htParams["STOCKCODE"] = StkCode;
                    }
                    if (AccountCode != 0) 
                    {
                        htParams["ACCOUNTCODE"] = AccountCode;
                    }

                    DataSet dsSource = _dao.QueryForTable("SnagDataProcedure.GetDataByProcedureName", htParams);
                    if (dsSource.Tables[0].Columns.Contains("LABELID"))
                    {
                        dsSource.Tables[0].Columns.Remove("LABELID");
                    }

                    dsSource.Tables[0].Rows.Add(dsSource.Tables[0].NewRow());

                    gvTableChart.DataSource = dsSource.Tables[0].DefaultView;
                    gvTableChart.DataBind();
                }
            }
            catch (Exception ex)
            {
                _log.Error(LabelID);
                _log.Error(ex.Message);
                _log.Error(ex.StackTrace);

                Response.Clear();
                Response.Write("数据源有错误");
                Response.End();
            }
        }
    }
}
